home *** CD-ROM | disk | FTP | other *** search
- Path: mac147.maths.uwa.edu.au!user
- From: ma@maths.uwa.edu.au (ma)
- Newsgroups: comp.lang.c
- Subject: #define, question from a beginner
- Followup-To: comp.lang.c
- Date: 1 Feb 1996 08:39:08 GMT
- Organization: Math Dept, Univ of W A
- Distribution: world
- Message-ID: <ma-010296161301@mac147.maths.uwa.edu.au>
- NNTP-Posting-Host: mac147.maths.uwa.edu.au
-
- The following is a hypothetical academic situation. Be grateful if you
- could help.
-
- Consider the following example: Line numbers are NOT part of the program.
-
- //===============================
- 1 #include <stdio.h>
- 2 void function(void);
- 3
- 4 main(void)
- 5 {
- 6 function();
- 7 }
- 8
- 9 void function(void)
- 10 {
- 11 short j=1,k=2;
- 12 printf("\ntesting macro\n");
- 13
- 14 printf("%d, ",j); // macro should include this first line
- 15 /* comment is part of the macro
- 16 the comment contains a few lines
- 17 */
- 18 printf("%d",k); // macro should include this last line
- 19
- 20 printf("\nend of testing\n");
- 21 }
- //***************************
-
- I can use #define to replace single lines#14 and 18. The following works.
-
- //===============================
- #define first printf("%d, ",j); // macro should include this first line
- #define last printf("%d",k); // macro should include this last line
-
- #include <stdio.h>
- void function(void);
-
- main(void)
- {
- function();
- }
-
- void function(void)
- {
- short j=1,k=2;
- printf("\ntesting macro\n");
-
- first
- /* comment is part of the macro
- the comment contains a few lines
- */
- last
-
- printf("\nend of testing\n");
- }
- //***************************
-
- QUESTION: Can I use one single macro fiveLines to replace lines from 14 to
- 18 inclusively? I tried the following but got rejected. Is there any way to
- get around it? Note that lines 14 and 18 contain local variables.
-
-
- //===============================
- #define fiveLines printf("%d, ",j); // macro should include this first
- line
- /* comment is part of the macro
- the comment contains a few lines
- */
- printf("%d",k); // macro should include this last line
-
-
- #include <stdio.h>
- void function(void);
-
- main(void)
- {
- function();
- }
-
- void function(void)
- {
- short j=1,k=2;
- printf("\ntesting macro\n");
-
- fiveLines
-
- printf("\nend of testing\n");
- }
- //***************************
-
- Of course, you may suggest that I should use another function listed below
- but that is NOT my question. My question again: How can we use one single
- macro-name to replace a paragraph of several lines involving local
- variables and comments? Can we do it in C++?
-
- //===============================
- void newFiveLines(short jj,short kk)
- {
- printf("%d, ",jj); // macro should include this first line
- /* comment is part of the macro
- the comment contains a few lines
- */
- printf("%d",kk); // macro should include this last line
- }
-
- void function(void)
- {
- short j=1,k=2;
- printf("\ntesting macro\n");
-
- newFiveLines(j,k);
-
- printf("\nend of testing\n");
- }
- //***************************
-
- =============
- Be grateful if you could reply to:
-
- ma@maths.uwa.edu.au
-
- Thank you in advance. Gratefully. ma
- =============
-